//
//  AddTaskSortViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/28.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  添加分类主界面

#import "AddTaskSortViewController.h"

@interface AddTaskSortViewController ()<UITextViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)UITextField *taskSortDetailTF;

@property(nonatomic,strong)UITextView *taskSortDesTextView;

@end

@implementation AddTaskSortViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor=WhiteColor;

    self.automaticallyAdjustsScrollViewInsets=NO;

    self.title=@"添加分类";

    [self.view addSubview:self.mainView];

    // 确认提交
    [AppUI initNavigationItem:self.navigationItem andWithText:@"确  认" andTextColor:WhiteColor andIsRight:YES withBlock:^{

        if (self.taskSortDetailTF.text.length==0)
        {
            [self.view makeToast:@"请输入分类名称" duration:.5 position:CSToastPositionCenter];
        }
        else if (self.taskSortDesTextView.text.length==0)
        {
            [self.view makeToast:@"请输入分类描述" duration:.5 position:CSToastPositionCenter];
        }
        else
        {
            if (self.taskSortDetailTF.text.length>16)
            {
                [self.view makeToast:@"分类名称太长了" duration:1 position:CSToastPositionCenter];
            }
            else
            {
                [API AddSortWithSortName:self.taskSortDetailTF.text memo:self.taskSortDesTextView.text success:^(id res) {

                    if ([res[@"rt"] isEqual:@(1)])
                    {
                        [self.view makeToast:@"添加成功" duration:1 position:CSToastPositionCenter];

                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                            [self.navigationController popViewControllerAnimated:NO];

                        });
                    }
                    else
                    {
                        [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                    }
                } failure:^(NSInteger stateCode) {
                    
                }];
            }
        }
    }];
}


-(UIView *)mainView
{
    if (!_mainView) {
        _mainView=[UIView new];
        _mainView=[[UIView alloc]initWithFrame:CGRectMake(0,64,APPW,0)];
        _mainView.backgroundColor=WhiteColor;
        setViewHeight_Add(_mainView,kmargin*2);

        // 分类名称
        UILabel *taskSortNameLabel=[self.mainView getLabelWithText:@"分类名称(不能修改)" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:taskSortNameLabel];
        taskSortNameLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*2);

        _taskSortDetailTF=[[UITextField alloc]initWithFrame:CGRectMake(kmargin*2,_mainView.frame.size.height,APPW-kmargin*4,kmargin*3)];
        _taskSortDetailTF.placeholder=@"请输入分类名称";
        _taskSortDetailTF.font=Font_Number(16);
        // 修改placeholder颜色
        [_taskSortDetailTF setValue:LightGreyColor forKeyPath:@"_placeholderLabel.textColor"];
        _taskSortDetailTF.delegate=self;
        _taskSortDetailTF.textColor=DarkGreyColor;
//        ViewBorderRadius(_taskSortDetailTF,2,1,MAIN_BACK_COLOR);
        [self.mainView addSubview:_taskSortDetailTF];
        setViewHeight_Add(_mainView,kmargin*4);

        // 分类描述
        UILabel *taskSortDesLabel=[self.mainView getLabelWithText:@"分类描述" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:taskSortDesLabel];
        taskSortDesLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*2);

        _taskSortDesTextView=[[UITextView alloc]initWithFrame:CGRectMake(kmargin*3/2,_mainView.frame.size.height,APPW-kmargin*3,APPH/5)];
        _taskSortDesTextView.placeholder=@"请输入分类描述";
        _taskSortDesTextView.delegate=self;
//        ViewBorderRadius(_taskSortDesTextView,2,1,MAIN_BACK_COLOR);
        _taskSortDesTextView.font=Font_Number(16);
        _taskSortDesTextView.textColor=DarkGreyColor;
        [self.mainView addSubview:_taskSortDesTextView];
        setViewHeight_Add(_mainView,APPH/5);
        
    }
    return _mainView;
}

// 点击return键时候键盘下去
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}


@end
